package com.skt.manage.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.skt.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.skt.manage.domain.Doctors;
import com.skt.manage.mapper.MedicalrecordsMapper;
import com.skt.manage.domain.Medicalrecords;
import com.skt.manage.service.IMedicalrecordsService;

/**
 * 患者病历Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-22
 */
@Service
public class MedicalrecordsServiceImpl implements IMedicalrecordsService 
{
    @Autowired
    private MedicalrecordsMapper medicalrecordsMapper;

    /**
     * 查询患者病历
     * 
     * @param recordId 患者病历主键
     * @return 患者病历
     */
    @Override
    public Medicalrecords selectMedicalrecordsByRecordId(Long recordId)
    {
        return medicalrecordsMapper.selectMedicalrecordsByRecordId(recordId);
    }

    /**
     * 查询患者病历列表
     * 
     * @param medicalrecords 患者病历
     * @return 患者病历
     */
    @Override
    public List<Medicalrecords> selectMedicalrecordsList(Medicalrecords medicalrecords)
    {
        return medicalrecordsMapper.selectMedicalrecordsList(medicalrecords);
    }

    /**
     * 新增患者病历
     * 
     * @param medicalrecords 患者病历
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMedicalrecords(Medicalrecords medicalrecords)
    {
        int rows = medicalrecordsMapper.insertMedicalrecords(medicalrecords);
        insertDoctors(medicalrecords);
        return rows;
    }

    /**
     * 修改患者病历
     * 
     * @param medicalrecords 患者病历
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMedicalrecords(Medicalrecords medicalrecords)
    {
        medicalrecordsMapper.deleteDoctorsByDoctorId(medicalrecords.getRecordId());
        insertDoctors(medicalrecords);
        return medicalrecordsMapper.updateMedicalrecords(medicalrecords);
    }

    /**
     * 批量删除患者病历
     * 
     * @param recordIds 需要删除的患者病历主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMedicalrecordsByRecordIds(Long[] recordIds)
    {
        medicalrecordsMapper.deleteDoctorsByDoctorIds(recordIds);
        return medicalrecordsMapper.deleteMedicalrecordsByRecordIds(recordIds);
    }

    /**
     * 删除患者病历信息
     * 
     * @param recordId 患者病历主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMedicalrecordsByRecordId(Long recordId)
    {
        medicalrecordsMapper.deleteDoctorsByDoctorId(recordId);
        return medicalrecordsMapper.deleteMedicalrecordsByRecordId(recordId);
    }

    /**
     * 新增医生信息管理信息
     * 
     * @param medicalrecords 患者病历对象
     */
    public void insertDoctors(Medicalrecords medicalrecords)
    {
        List<Doctors> doctorsList = medicalrecords.getDoctorsList();
        Long recordId = medicalrecords.getRecordId();
        if (StringUtils.isNotNull(doctorsList))
        {
            List<Doctors> list = new ArrayList<Doctors>();
            for (Doctors doctors : doctorsList)
            {
                doctors.setDoctorId(recordId);
                list.add(doctors);
            }
            if (list.size() > 0)
            {
                medicalrecordsMapper.batchDoctors(list);
            }
        }
    }
}
